-- SpecialEquipM
-- Create by weism
-- 附加属性功能模块

module("SpecialEquipM", package.seeall);

local specialEquip = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    specialEquip = {};

    -- 转换下special_equip信息表
    local special_equip_CSV = get_special_equip_CSV();
    for i = 1, #special_equip_CSV do
        specialEquip[special_equip_CSV[i].class_id] = special_equip_CSV[i];
    end
end

-- 模块初始化
function init()
    loadCsv();

    EventMgr.removeAll("SpecialEquipM");
end

function query(classId, path)
    local m = specialEquip[classId];

    if not m or not path then
        return m;
    end

    return m[path];
end

-- 是否专属装备
function isSpecialEquip(classId)
    if not specialEquip[classId] then
        return false;
    end

    return true;
end

-- 获取专属属性
function fetchEquipProps(classId)
    -- 如果不是专属装备
    if not isSpecialEquip(classId) then
        return {};
    end

    local special = query(classId, "special");
    local level = ME.user:getSkillLevel(special);
    local ret = {};

    -- 基础属性
    local prop = query(classId, "base");
    if type(prop) == 'table' then
        table.insert(ret, table.deepcopy(prop))
    end

    -- 等级附加的属性
    for i = 1, level do
        local path = "lv" .. i;
        prop = query(classId, path);

        if prop then
            table.insert(ret, table.deepcopy(prop));
        end
    end

    return ret;
end

-- 获取描述
function getSpecialDesc(classId)
    if not isSpecialEquip(classId) then
        return "";
    end

    local special = query(classId, "special");

    -- 基本描述
    local desc = query(classId, "desc") or "";
    local prop =  query(classId, "base");
    local value;
    if type(prop) == 'table' then
        -- 需要动态显示数值的
        value = prop[3];
        if PropM.getRow(prop[1])["type"] == PROP_TYPE_RATIO then
            value = value / 10;
        end
        desc = string.gsub(desc, "{prop}", value);
    end

    -- 子项描述
    local childDesc = query(classId, "child_desc");
    local tempDesc = "";
    for i = 1, 5 do
        desc = desc .. "\n";
        desc = desc .. string.format(getLocStr("brace_and_text"), spliceLocStr(SkillM.query(special, "name"), string.format(getLocStr("level_format"), i)));
        -- 如果有专门配置了特殊描述
        if #childDesc > 0 then
            prop = query(classId, "lv" .. i);
            value = prop[3];
            if PropM.getRow(prop[1])["type"] == PROP_TYPE_RATIO then
                value = value / 10;
            end

            if type(childDesc) == "table" then
                tempDesc = childDesc[i];
            elseif type(childDesc) == "string" then
                tempDesc = childDesc;
            end
            tempDesc = string.gsub(tempDesc, "{prop}", value);
            desc = spliceLocStr(desc, tempDesc);
        else
            desc = spliceLocStr(desc, PropM.getPropDesc(query(classId, "lv" .. i)));
        end
    end

    return desc;
end
